import React from 'react';
class Car extends React.Component {
	constructor() {
			super()
			this.state={
				arr:[],
				xuanall:1
			}
	}
	componentDidMount(){
		if(window.localStorage.gwc){
			this.setState({
				arr:JSON.parse(window.localStorage.gwc)
			})
		}
	}
	//数量加
	jia(ind){
		let arr = this.state.arr
		arr[ind].shu ++
		this.setState({
			arr
		})
		window.localStorage.gwc = JSON.stringify(this.state.arr)
	}
	//数量减
	jian(ind){
		let arr = this.state.arr
		if(arr[ind].shu <= 0){
			arr[ind].shu = 0
		}else{
			arr[ind].shu --
		}
		this.setState({
			arr
		})
		window.localStorage.gwc = JSON.stringify(this.state.arr)
	}
<<<<<<< HEAD
	check(){
		
=======
	// 单个选择
	change(index,e){
		var arr = this.state.arr
		var z = 0
		if(arr[index].xuank == ''){
			arr[index].xuank = '1'
			for(let i=0;i<arr.length;i++){
				z += Number(arr[i].xuank)
			}
			if(z == arr.length){
				this.setState({
					xuanall:1
				})
			}
			this.setState({
				arr:arr
			})
		}else{
			arr[index].xuank = ''
			for(let i=0;i<arr.length;i++){
				z += Number(arr[i].xuank)
			}
			if(z != arr.length){
				this.setState({
					xuanall:''
				})
			}
			this.setState({
				arr:arr
			})
		}
	}
	// 全选不选
	changeAll(){
		var xuanall = this.state.xuanall
		var arr = this.state.arr
		if(xuanall == ''){
			for(let i=0;i<arr.length;i++){
				arr[i].xuank = '1'
			}
			this.setState({
				xuanall:'1',
				arr:arr
			})
		}else{
			for(let i=0;i<arr.length;i++){
				arr[i].xuank = ''
			}
			this.setState({
				xuanall:'',
				arr:arr
			})
		}
>>>>>>> 02b69c8bc3d30aaaa94bbd11a54f16d3ad48afa6
	}
	render() {
		var arr = this.state.arr;
		//计算总价
		var zong = 0;
		if(arr.length != 0){
			if(this.state.xuanall == 1){
				for(let i=0;i<arr.length;i++){
					zong += arr[i].shu * arr[i].productprice
				}
			}else{
				for(let i=0;i<arr.length;i++){
					if(arr[i].xuank == 1){
						zong += arr[i].shu * arr[i].productprice
					}
				}
			}
		}
		var arr1=arr.map((key,index)=>{
			return (
				<li className="showLi" key={index}>
<<<<<<< HEAD
				
=======
					<input type="checkbox" className="inp" id={index} checked={key.xuank} onChange={this.change.bind(this,index)}/><label htmlFor={index}></label>
>>>>>>> 02b69c8bc3d30aaaa94bbd11a54f16d3ad48afa6
					<img src={key.thumb} />
					<div className="price_list">
							<div>{key.title}</div>
							<div className="price">
									<span className="money">{key.productprice}</span>
									<span className="num">
									
										<span className="num_add" onClick={this.jia.bind(this,index)}>+</span>
										<span className="num_num">{key.shu}</span>
										<span className="minus" onClick={this.jian.bind(this,index)}>-</span>
									</span>
							</div>
					</div>
				</li>
			)
		})
		return (
			<div>
				<ul className="ul">
				
					{arr1}
				</ul>
				<div className="bottom">
<<<<<<< HEAD
					
					<span className="jiage">总价:{zong}</span>
=======
					<input type="checkbox" className="inp" id='qb' checked={this.state.xuanall} onChange={this.changeAll.bind(this)}/><label className='label' htmlFor="qb"></label>
					<span className="jiage">
						总价:{zong}
					</span>
>>>>>>> 02b69c8bc3d30aaaa94bbd11a54f16d3ad48afa6
					<span className="pay">立即支付</span>
				</div>
			</div>
		)
	}
}
export default Car;